package com.kcsj.controller.basic;
//201902104020 李鑫元
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.WayBill;
import com.kcsj.service.WayBillService;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

@Slf4j
@WebServlet("/wayBill.ctl")
public class WayBillController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取前端请求的信息
        String orderNo = request.getParameter("orderNo");
        Integer id= Integer.parseInt(request.getParameter("userId"));
        try {
            if(orderNo!=null){
                this.responseWayBill(orderNo, response);
            }else {
                this.responseWayBills(id, response);
            }

        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            //响应message到前端
            response.getWriter().println(message);
            e.printStackTrace();

        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();

            //响应message到前端
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }

    private void responseWayBill(String orderNo, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        WayBill wayBill = WayBillService.getInstance().find(orderNo);
        String order_json = JSON.toJSONString(wayBill);
        JSONObject data = MessageHelper.addData(order_json);
        //响应department_json到前端
        response.getWriter().println(data);
    }

    private void responseWayBills(Integer id, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //获得所有学生
        Collection<WayBill> wayBills = WayBillService.getInstance().findAll(id);
        String taskLists_json = JSON.toJSONString(wayBills, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(taskLists_json);
        //响应departments_json到前端
        response.getWriter().println(data);
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String wayBill_json = JSONUtil.getJSON(request);
        //将JSON字串解析为TaskList对象
        WayBill wayBillToAdd = JSON.parseObject(wayBill_json, WayBill.class);
        //创建JSON对象message，以便往前端响应信息
        //到数据库表修改WayBill对象对应的记录
        try {
            WayBillService.getInstance().update(wayBillToAdd);
            JSONObject message = MessageHelper.addMessage("修改成功");
            response.getWriter().println(message);
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            //响应message到前端
            response.getWriter().println(message);
            e.printStackTrace();

        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();

            //响应message到前端
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //读取参数no
        String wayBill_json = JSONUtil.getJSON(request);
        WayBill wayBillToDel = JSON.parseObject(wayBill_json, WayBill.class);
        //到数据库表中删除对应的任务单
        try {
            JSONObject message = MessageHelper.addMessage("修改成功");
            WayBillService.getInstance().delete(wayBillToDel.getWayBillNo());
            response.getWriter().println(message);
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            //响应message到前端
            response.getWriter().println(message);
            e.printStackTrace();
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            //响应message到前端
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }

}
